package com.leador.map.api.location;

import com.leador.map.api.location.core.ClientInfoUtil;
import com.leador.map.api.location.core.GCJProjection;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Message;

/**
 *
 */
public class GpsLocationManager {
	private static GpsLocationManager _instance = null;

	private LocationManager androidLocManager = null;

	private MapLocationManager.MapLocationHandler mapLocationHandler;
	private Context context;
	private ClientInfoUtil clientInfo;
	private String mcc;

    /**
     * 构造函数
     * @param context
     * @param androidLocManager
     * @param mapLocationHandler
     */
	private GpsLocationManager(Context context, LocationManager androidLocManager,
							   MapLocationManager.MapLocationHandler mapLocationHandler) {
		this.androidLocManager = androidLocManager;
		this.mapLocationHandler = mapLocationHandler;
		this.context = context;
		clientInfo = ClientInfoUtil.getInstance(context);
		mcc = clientInfo.getMcc(context);
	}

    /**
     * 获取单例实例
     * @param context
     * @param androidLocManager
     * @param mapLocationHandler
     * @return
     */
	public static GpsLocationManager getInstance(Context context,
			LocationManager androidLocManager,
			MapLocationManager.MapLocationHandler mapLocationHandler) {
        synchronized (GpsLocationManager.class) {
            if (_instance == null) {
                _instance = new GpsLocationManager(context, androidLocManager,
                        mapLocationHandler);
            }
        }
		return _instance;
	}

    /**
     * 请求GPS更新位置
     *
     * @param minTime
     * @param minDistance
     * @param listener
     * @param provider
     */
	public void requestLocationUpdates(long minTime, float minDistance,
			LocationListener listener, String provider) {
		try {
			if (androidLocManager
					.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
				androidLocManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER, minTime, minDistance,
						gpsListener);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void removeGPSUpdates() {
		androidLocManager.removeUpdates(gpsListener);
	}

	private LocationListener gpsListener = new LocationListener() {

		@Override
		public void onLocationChanged(final Location location) {
			MapLocationManager.mGpsFixed = true;
			MapLocationManager.mLastGpsFixedTime = System.currentTimeMillis();
			MapLocation aloc = null;
			try {
				if (clientInfo.isInMainLand(mcc)) {
                    //GPS坐标

					double[] offSetLoc =new double[2];
					GCJProjection.project(location.getLongitude(), location.getLatitude(), offSetLoc);
					//
					aloc = new MapLocation(location);
					aloc.setLatitude(offSetLoc[1]);
					aloc.setLongitude(offSetLoc[0]);
				} else {
					aloc = new MapLocation(location);
				}
			} catch (Exception e) {

			} finally {
				Message msg = new Message();
				msg.obj = aloc;
				msg.what = MapLocationManager.LOCATION_MESSAGE_SUCCESS;
                //
				mapLocationHandler.sendMessage(msg);
                //
				MapLocationManager.mGpsFixed = true;
				MapLocationManager.mLastGpsFixedTime = System.currentTimeMillis();
			}

		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};
}
